package com.yyk.yykywd.ui.work.fragment;


import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.orhanobut.hawk.Hawk;
import com.orhanobut.logger.Logger;
import com.yyk.yykywd.R;
import com.yyk.yykywd.base.BaseFragment;
import com.yyk.yykywd.http.ApiService;
import com.yyk.yykywd.bean.WorkIndexInfo;
import com.yyk.yykywd.ui.work.BusinessGoActivity;
import com.yyk.yykywd.ui.work.BusinessPlanActivity;
import com.yyk.yykywd.ui.work.CooperationCgActivity;
import com.yyk.yykywd.ui.work.CooperationYetActivity;
import com.yyk.yykywd.ui.work.CustomerCallActivity;
import com.yyk.yykywd.ui.work.ReceivableActivity;
import com.yyk.yykywd.ui.work.SignedContractActivity;
import com.yyk.yykywd.ui.work.WorkApplylistActivity;
import com.yyk.yykywd.ui.work.WorkPactlistActivity;
import com.yyk.yykywd.util.Md5Util2;
import com.yyk.yykywd.util.OnClickFlagUtil;
import com.yyk.yykywd.util.ToastUtil;

import java.util.Map;
import java.util.TreeMap;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;


/**
 * 工作 fragment
 */
public class WorkFragment extends BaseFragment {
    public static final String TAG = "WorkFragment";
    @BindView(R.id.iv_bone)
    ImageView ivBone;
    @BindView(R.id.iv_two)
    ImageView ivTwo;
    @BindView(R.id.iv_bthree)
    ImageView ivBthree;
    @BindView(R.id.iv_bfour)
    ImageView ivBfour;
    @BindView(R.id.iv_bfive)
    ImageView ivBfive;
    @BindView(R.id.iv_cone)
    ImageView ivCone;
    @BindView(R.id.iv_ctwo)
    ImageView ivCtwo;
    @BindView(R.id.iv_cthree)
    ImageView ivCthree;
    @BindView(R.id.iv_cfour)
    ImageView ivCfour;
    @BindView(R.id.iv_cfive)
    ImageView ivCfive;
    @BindView(R.id.ll_ywtjone)
    LinearLayout llYwtjone;
    @BindView(R.id.ll_ywtjtwo)
    LinearLayout llYwtjtwo;
    @BindView(R.id.ll_bone)
    LinearLayout llBone;
    @BindView(R.id.ll_btwo)
    LinearLayout llBtwo;
    @BindView(R.id.ll_bthree)
    LinearLayout llBthree;
    @BindView(R.id.ll_bfour)
    LinearLayout llBfour;
    @BindView(R.id.ll_bfive)
    LinearLayout llBfive;
    @BindView(R.id.tv_apply)
    TextView tvApply;
    @BindView(R.id.tv_pact)
    TextView tvPact;
    @BindView(R.id.tv_not)
    TextView tvNot;
    @BindView(R.id.tv_success)
    TextView tvSuccess;


    @Override
    public int getLayoutId() {
        return R.layout.fragment_work;
    }

    @Override
    public void initView() {

    }

    @Override
    public void initData() {
//        initPost();
    }

    /**
     * Fragmetn点击切换会调用这个方法
     *
     * @param hidden
     */
    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (hidden) {   // 不在最前端显示 相当于调用了onPause();

        } else {  // 在最前端显示 相当于调用了onResume();
            initPost();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        initPost();
    }

    private void initPost() {
        /**
         * 接口22：工作模块个数（whm）
         */
        Map<String, String> map = new TreeMap<>();
        map.put("uid", Hawk.get("uid") + "");
        map.put("sign", Md5Util2.createSign(map));
        ApiService.getInstance().api.postWorkIndex(map)
                .subscribeOn(Schedulers.io())     //调度线程
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())   //取消订阅
                .subscribe(new Observer<WorkIndexInfo>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(WorkIndexInfo info) {
                        Logger.e("工作模块个数" + info.toString());
                        if (info.getCode() == 1) {
                            tvNot.setText(info.getData().getNot() + "");
                            tvApply.setText(info.getData().getApply() + "");
                            tvPact.setText(info.getData().getPact() + "");
                            tvSuccess.setText(info.getData().getSuccess() + "");
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }

    /**
     * 1.业务计划
     * 2.客户拜访
     * 3.暂无意向
     * 4.待签合同
     * 5.添加回款
     * 6.  合作暂未达成
     * 7.合作成功
     * 8.项目申请书列表
     * 9.合同列表
     *
     * @param view
     */
    @OnClick({R.id.ll_bone, R.id.ll_btwo, R.id.ll_bthree, R.id.ll_bfour, R.id.ll_bfive, R.id.ll_ywtjone, R.id.ll_ywtjtwo, R.id.ll_apply, R.id.ll_pact,
            R.id.ll_cone, R.id.ll_ctwo, R.id.ll_cthree, R.id.ll_cfour, R.id.ll_cfive})
    public void onViewClicked(View view) {
        if (OnClickFlagUtil.isFastClick()) {
            return;
        }
        switch (view.getId()) {
            case R.id.ll_bone:
                startAct(BusinessPlanActivity.class);
                break;
            case R.id.ll_btwo:
                startAct(CustomerCallActivity.class);
                break;
            case R.id.ll_bthree:
                startAct(BusinessGoActivity.class);
                break;
            case R.id.ll_bfour:
                startAct(SignedContractActivity.class);
                break;
            case R.id.ll_bfive:
                startAct(ReceivableActivity.class);
                break;
            case R.id.ll_ywtjone:
                startAct(CooperationYetActivity.class);
                break;
            case R.id.ll_ywtjtwo:
                startAct(CooperationCgActivity.class);
                break;
            case R.id.ll_apply:
                startAct(WorkApplylistActivity.class);
                break;
            case R.id.ll_pact:
                startAct(WorkPactlistActivity.class);
                break;
            case R.id.ll_cone:
                ToastUtil.showShort("功能暂未开放");
                break;
            case R.id.ll_ctwo:
                ToastUtil.showShort("功能暂未开放");
                break;
            case R.id.ll_cthree:
                ToastUtil.showShort("功能暂未开放");
                break;
            case R.id.ll_cfour:
                ToastUtil.showShort("功能暂未开放");
                break;
            case R.id.ll_cfive:
                ToastUtil.showShort("功能暂未开放");
                break;
        }
    }


}
